package com.uyayo.system.authority.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.uyayo.system.authority.entity.SysOrganizationResource;
import com.uyayo.system.authority.entity.SysRoleResource;
import com.uyayo.system.authority.mapper.SysRoleMapper;
import com.uyayo.system.authority.model.SysRoleCondition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uyayo.framework.common.exception.CustomException;
import com.uyayo.framework.common.util.CodeUtil;
import com.uyayo.framework.pagehelper.pojo.OrderBy;
import com.uyayo.framework.pagehelper.pojo.PageBean;
import com.uyayo.framework.pagehelper.util.OrderByUtil;
import com.uyayo.system.authority.entity.SysRole;
import com.uyayo.system.authority.mapper.SysRoleResourceMapper;
import com.uyayo.system.authority.service.SysRoleService;

@Service("sysRoleService")
public class SysRoleServiceImpl  implements SysRoleService {

	private final SysRoleMapper roleMapper;
	
	private final SysRoleResourceMapper roleResourceMapper;

	@Autowired
	public SysRoleServiceImpl(SysRoleMapper roleMapper, SysRoleResourceMapper roleResourceMapper) {
		this.roleMapper = roleMapper;
		this.roleResourceMapper = roleResourceMapper;
	}


	@Override
	public SysRole getRoleById(String id) throws Exception{
		SysRole role =roleMapper.selectByPrimaryKey(id);
		//中间对商品信息进行业务处理
		if(role==null){
			throw new CustomException("该角色信息不存在！");
		}
		return role;
	}
	
	@Override
	public List<SysRole> selectRole(SysRole role) {
		return roleMapper.select(role);
	}
	

	@Override
	public PageBean<SysRole> selectRole(Integer pageCurrent, Integer pageSize,String sort,String order,SysRoleCondition sysRoleQUERY) {
		List<SysRole> rolesList;
		//分页处理
		List<OrderBy> orderBies = new ArrayList<>();
		orderBies.add(new OrderBy("createdTime", "created_time"));
		orderBies.add(new OrderBy("modifiedTime", "modified_time"));
		orderBies.add(new OrderBy("seq", "seq"));
		OrderByUtil  orderByUtil = new OrderByUtil();
		String orderBy = orderByUtil.getOrderBy(orderBies, sort, order);
		PageHelper.startPage(pageCurrent, pageSize);
		
		Example example = new Example(SysRole.class);
		if(!StringUtils.isEmpty(orderBy)){
			example.setOrderByClause(orderBy);
		}
		Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(sysRoleQUERY.getName())){
			criteria.andLike("name","%"+sysRoleQUERY.getName()+"%");
		}
		if(sysRoleQUERY.getBeginCreatedTime()!=null || sysRoleQUERY.getEndCreatedTime()!=null){
			criteria.andBetween("createdTime", sysRoleQUERY.getBeginCreatedTime(), sysRoleQUERY.getEndCreatedTime());
		}
		
		rolesList = roleMapper.selectByExample(example);
		
		//取记录总条数
		PageInfo<SysRole> pageInfo = new PageInfo<>(rolesList);		
		return new PageBean<>(pageInfo.getTotal(), pageCurrent, pageSize, rolesList);
	}


	@Override
	public int saveRole(SysRole role) {
		return roleMapper.insertSelective(role);
	}

	@Override
	public SysRole getRoleByUpdateTestOnly(String id) {
		return roleMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateRole(SysRole role) {
		return roleMapper.updateByPrimaryKeySelective(role);
	}

	@Override
	public int deleteRole(String id) {
		Example exampleO = new Example(SysRoleResource.class);
		Example.Criteria criteriaO = exampleO.createCriteria();
		criteriaO.andEqualTo("roleId", id);
		roleResourceMapper.deleteByExample(exampleO);
		return roleMapper.deleteByPrimaryKey(id);
	}
	
	
	@Override
	public List<SysRoleResource> findRoleResources(String roleId) {
		List<SysRoleResource> roleResources = new ArrayList<>();
		try {
			Example example = new Example(SysRoleResource.class);  
			Criteria criteria = example.createCriteria();
			criteria.andEqualTo("roleId", roleId);
			roleResources = roleResourceMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return roleResources;
	}
	
	@Override
	public int grantRoleResource(String ids,String roleId) {

		Example exampleO = new Example(SysRoleResource.class);
		Example.Criteria criteriaO = exampleO.createCriteria();
		criteriaO.andEqualTo("roleId", roleId);
		roleResourceMapper.deleteByExample(exampleO);

		int saveCount = 0;
		String[] resourceIdArr = ids.split(",");
		List<SysRoleResource> roleResources = new ArrayList<>();
		if(resourceIdArr.length>0 && !resourceIdArr[0].equals("")){
			for (String aResourceIdArr : resourceIdArr) {
				SysRoleResource roleResource = new SysRoleResource();
				roleResource.setId(CodeUtil.getTimeSequenceAsRandomCode());
				roleResource.setRoleId(roleId);
				roleResource.setResourceId(aResourceIdArr);
				roleResources.add(roleResource);
			}
		}
		Example example = new Example(SysRoleResource.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("roleId", roleId);
		if(roleResources.size()>0){
			saveCount = roleResourceMapper.insertRoleResourceByList(roleResources);
		}
		
		return saveCount;
	}

	@Override
	public List<SysRole> findRoleByUserId(String userId) {
		List<SysRole> roleList = new ArrayList<>();
		try {
			roleList = roleMapper.selectRoleByUserId(userId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return roleList;
	}
}
